package com.example.flower_shop.service.usr.impl;

import cn.dev33.satoken.stp.StpUtil;
import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.flower_shop.common.PageResult;
import com.example.flower_shop.entity.UserCollect;
import com.example.flower_shop.exception.exception;
import com.example.flower_shop.mapper.UserCollectMapper;
import com.example.flower_shop.service.usr.UserCollectService;
import com.example.flower_shop.utils.DecimalUtil;
import com.example.flower_shop.vo.collect.CollectPageReq;
import com.example.flower_shop.vo.collect.CollectReq;
import com.example.flower_shop.vo.collect.CollectRespVO;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

/**
 * @author zbxComputer
 * @description 针对表【sys_user_collect(用户收藏商品表)】的数据库操作Service实现
 * @createDate 2024-11-21 12:48:27
 */
@Service
public class UserCollectServiceImpl extends ServiceImpl<UserCollectMapper, UserCollect>
        implements UserCollectService {

    @Resource
    private UserCollectMapper userCollectMapper;

    @Override
    public void addCollect(CollectReq req) {
        LambdaQueryWrapper<UserCollect> wrapper = new LambdaQueryWrapper<>();
        long userId = StpUtil.getLoginIdAsLong();
        wrapper.eq(UserCollect::getUid, userId)
                .eq(UserCollect::getProductId, req.getProductId());
        if (count(wrapper) > 0) {
            throw new exception("已收藏");
        }
        UserCollect collect = BeanUtil.toBean(req, UserCollect.class);
        collect.setUid(userId);
        if (!save(collect)) {
            throw new exception("收藏失败");
        }
    }

    @Override
    public PageResult<CollectRespVO> getCollectList(CollectPageReq req) {
        Page<CollectRespVO> page = new Page<>(req.getPage(), req.getSize());
        IPage<CollectRespVO> iPage = userCollectMapper.pageQuery(page, req, StpUtil.getLoginIdAsLong());
        return PageResult.of(iPage.getTotal(), iPage.getCurrent(), iPage.getSize(), iPage.getRecords()
                .stream()
                .peek(e -> {
                    e.setVipPrice(DecimalUtil.vip(e.getPrice()));
                })
                .toList());
    }

    @Override
    public void deleteCollect(Long id) {
        LambdaQueryWrapper<UserCollect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(UserCollect::getUid, StpUtil.getLoginIdAsLong())
                .and(w ->
                        w.eq(UserCollect::getId, id)
                                .or()
                                .eq(UserCollect::getProductId, id));
        if (!remove(queryWrapper)) {
            throw new exception("取消收藏失败");
        }
    }

    @Override
    public Boolean hasCollected(Long productId) {
        if (!StpUtil.isLogin()) {
            return false;
        }
        LambdaQueryWrapper<UserCollect> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UserCollect::getUid, StpUtil.getLoginIdAsLong())
                .eq(UserCollect::getProductId, productId);
        return exists(wrapper);
    }
}




